﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Hidden_Picture
{
    public partial class Form1 : Form
    {
        public static Bitmap picture1;
        public static Bitmap picture2;
        public static Bitmap result;
        public static Set set = new Set();
        public static Wait wait = new Wait();
        public static Form1 main;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            main = this;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Bitmap p = PictureIO.GetBitmap();
            if(p != null)
            {
                pictureBox1.Image = p;
                button3.Enabled = button4.Enabled = false;
                button4.Text = "原图";
                if (picture1 != null)
                {
                    picture1.Dispose();
                }
                picture1 = p;
                if (set.radioButton1.Checked)
                {
                    set.numericUpDown5.Value = p.Width;
                    set.numericUpDown6.Value = p.Height;
                }
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Bitmap p = PictureIO.GetBitmap();
            if(p != null)
            {
                pictureBox2.Image = p;
                button3.Enabled = button4.Enabled = false;
                button4.Text = "原图";
                if (picture2 != null)
                {
                    picture2.Dispose();
                }
                picture2 = p;
                if (set.radioButton2.Checked)
                {
                    set.numericUpDown5.Value = p.Width;
                    set.numericUpDown5.Value = p.Height;
                }
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(picture1 == null)
            {
                ShowDialog("请选择一张白图!");
                return;
            }
            if(picture2 == null)
            {
                ShowDialog("请选择一张黑图!");
                return;
            }
            Thread thread = new Thread(Start);
            thread.Start();
            wait.Start();
            wait.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            set.ShowDialog();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                pictureBox1.SizeMode = pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            }
            else
            {
                pictureBox1.SizeMode = pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        public void Start()
        {
            if (set.radioButton1.Checked)
            {
                PictureIO.FinalWidth = picture1.Width;
                PictureIO.FinalHeight = picture1.Height;
            }
            else if (set.radioButton2.Checked)
            {
                PictureIO.FinalWidth = picture2.Width;
                PictureIO.FinalHeight = picture2.Height;
            }
            else
            {
                PictureIO.FinalWidth = (int)set.numericUpDown5.Value;
                PictureIO.FinalHeight = (int)set.numericUpDown6.Value;
            }
            Bitmap result = PictureIO.GetResult(picture1, picture2);
            if (result == null) return;
            if (Form1.result != null) Form1.result.Dispose();
            Form1.result = result;
            pictureBox1.Image = pictureBox2.Image = Form1.result;
            button3.Enabled = button4.Enabled = true;
            wait.close();
        }

        private void ShowDialog(string s)
        {
            MessageBox.Show(s, "QQ隐藏图",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(result == null)
            {
                ShowDialog("没有生成结果!");
                return;
            }
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "PNG图片|*.png";
            if(save.ShowDialog() == DialogResult.OK)
            {
                result.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Png);
                MessageBox.Show("保存成功!", "QQ隐藏图");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(button4.Text == "原图")
            {
                pictureBox1.Image = picture1;
                pictureBox2.Image = picture2;
                button4.Text = "重现";
            }
            else
            {
                pictureBox1.Image = pictureBox2.Image = result;
                button4.Text = "原图";
            }
        }
    }
}
